cmake_minimum_required(VERSION 3.8)
set(moduleName camera_perception_b)

find_package(rclcpp REQUIRED)
find_package(interface REQUIRED)

find_package(CUDA REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(cv_bridge REQUIRED)
set(CMAKE_CXX_STANDARD 17)

list(APPEND CUDA_NVCC_FLAGS "-std=c++17 -g")

if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL  "aarch64")
  set(CUDA_INSTALL_TARGET_DIR targets/aarch64-linux)
elseif(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  set(CUDA_INSTALL_TARGET_DIR targets/x86_64-linux)
endif()

set(CUDA_TOOLKIT_ROOT_DIR /usr/local/cuda)
set(CUDA_INCLUDE_DIRS ${CUDA_TOOLKIT_ROOT_DIR}/${CUDA_INSTALL_TARGET_DIR}/include)
set(CUDA_LIB_DIRS ${CUDA_TOOLKIT_ROOT_DIR}/${CUDA_INSTALL_TARGET_DIR}/lib)

set(CMAKE_BUILD_TYPE "RELEASE")

set(CMAKE_CXX_FLAGS_RELEASE "-Wno-deprecated-declarations -O2 -g")

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}
  -ccbin ${CMAKE_CXX_COMPILER}
)

if(${CMAKE_BUILD_TYPE} STREQUAL "DEBUG")
  message("Using Debug Mode")
  set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -g -G --ptxas-options=-v)
endif()

if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL  "aarch64")       # orin
  set(TENSORRT_INCLUDE_DIRS ./tensorrt/include/aarch64-linux-gnu/)
  set(TENSORRT_LIBRARY_DIRS ./tensorrt/lib/aarch64-linux-gnu/)
elseif(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  # set(TENSORRT_INCLUDE_DIRS /root/TensorRT-8.5.2.2/include)  # your tensorrt path
  # set(TENSORRT_LIBRARY_DIRS /root/TensorRT-8.5.2.2/lib)
endif()

set(CUDA_LIB_DIRS ${CUDA_TOOLKIT_ROOT_DIR}/${CUDA_INSTALL_TARGET_DIR}/lib)
find_library(NVJPEG_LIBRARY nvjpeg ${CUDA_LIB_DIRS})

if(NVJPEG_LIBRARY)
  add_definitions(-D__HAVE_NVJPEG__)
  link_libraries(${NVJPEG_LIBRARY})
  message(STATUS ${NVJPEG_LIBRARY})
endif()

include_directories(
  ${CUDA_INCLUDE_DIRS}
  ${TENSORRT_INCLUDE_DIRS}
  ${YAML_CPP_INCLUDE_DIRS}
  ${EIGEN3_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

link_directories(${TENSORRT_LIBRARY_DIRS})

include(../../common/cmake/add3rd.cmake)
add3rds(math muduo)

file (GLOB SRCS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" 
  byte_tracker/*.cpp 
  pose_detector/*.cpp 
  reid/*.cpp 
  lane_detector/*.cpp
  lane_seg/*.cpp
  *.cpp)
cuda_add_executable(${moduleName} ${SRCS})
include_directories(../../common)
target_link_libraries(${moduleName} 
    libnvinfer.so
    nvinfer_plugin.so
    ${CUDA_LIBRARIES}
    ${OpenCV_LIBS}
    math
    muduo_net 
    muduo_base
)

ament_target_dependencies(${moduleName} rclcpp interface cv_bridge)
install(TARGETS ${moduleName} DESTINATION lib/${PROJECT_NAME})
